package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.constant.user.AdminConstans;
import com.usian.model.article.pojo.ApAuthor;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ArticleFeign;
import com.usian.user.feign.WemediaFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.ApUserRealnameMapper;
import com.usian.user.service.ApUserRealnameService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUserRealNameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {
    @Autowired
    private ArticleFeign articleFeign;

    @Autowired
    private WemediaFeign wemediaFeign;

    @Autowired
    private ApUserMapper apUserMapper;

    @Override
    public PageResponseResult loadListByStatus(AuthDto dto) {
        if(dto == null)return (PageResponseResult) ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        LambdaQueryWrapper<ApUserRealname> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApUserRealname::getStatus,dto.getStatus());
        Page page = new Page(dto.getPage(), dto.getSize());
        IPage iPage = page(page, wrapper);
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) iPage.getTotal());
        result.setCode(0);
        result.setErrorMessage("查询成功");
        result.setData(iPage.getRecords());
        return result;
    }

    @Override
    /*@Transient*/
    public ResponseResult updateStatusById(AuthDto dto) {
        if(dto == null || dto.getId() == null || dto.getStatus() == null || !dto.getStatus().equals(AdminConstans.PASS_AUTH))return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        ApUserRealname realname = new ApUserRealname();
        realname.setId(dto.getId());
        realname.setStatus(dto.getStatus());
        if(dto.getMsg() != null){
            realname.setReason(dto.getMsg());
        }
        updateById(realname);
        if(dto.getStatus().equals(AdminConstans.PASS_AUTH)){
            ApUserRealname rz = getById(dto.getId());
            ApUser apUser = apUserMapper.selectById(rz.getUserId());
            WmUser wm = wemediaFeign.findByName(apUser.getName());
            if(wm == null || wm.getId() == null){
                WmUser wmUser = new WmUser();
                BeanUtils.copyProperties(apUser,wmUser);
                wmUser.setApUserId(apUser.getId());
                wmUser.setCreatedTime(new Date());
                wmUser.setStatus(AdminConstans.PASS_AUTH.intValue());
                wemediaFeign.save(wmUser);

                ApAuthor author = articleFeign.findByUserId(apUser.getId());
                if(author == null){
                    ApAuthor apAuthor = new ApAuthor();
                    apAuthor.setName(apUser.getName());
                    apAuthor.setType(2);
                    apAuthor.setCreatedTime(new Date());
                    apAuthor.setUserId(apUser.getId());
                    WmUser newWmUser = wemediaFeign.findByName(apUser.getName());
                    apAuthor.setWmUserId(newWmUser.getId());
                    articleFeign.save(apAuthor);
                    ApAuthor newAuthor = articleFeign.findByUserId(apUser.getId());
                    newWmUser.setApAuthorId(newAuthor.getId());
                    apUser.setFlag(Short.valueOf("1"));
                    apUserMapper.updateById(apUser);
                }
            }
            return ResponseResult.okResult(0,"认证通过");
        }
        return ResponseResult.okResult(0,"拒绝通过");
    }
}
